from django.shortcuts import render

# Create your views here.
#coding=utf-8
from django.shortcuts import render,render_to_response
from django.http import HttpResponse
from django import forms
from c_1.models import User

#登录
# def login(request):
#     if request.method=="GET":
#         ip=request.META['REMOTE_ADDR']
#         return  render(request,'login.html',{'ip':ip})
#     elif request.method=="POST":
#         username=request.POST.get('username','123')
#         pwd=request.POST.get('pwd','123')
#         s='username:'+username+'<br>pwd:'+pwd
#     return redirect('./home')
#主页
def home(request):
    return  render(request,'home.html')
# #注册
# def zhuce(request):
#     return render(request,'zhuce1.html')

class UserForm(forms.Form):
    username = forms.CharField(label='用户名',max_length=50)
    password = forms.CharField(label='密码',widget=forms.PasswordInput())
    email = forms.EmailField(label='邮箱')

def regist(request):
    if request.method == 'POST':
        userform = UserForm(request.POST)
        if userform.is_valid():
            username = userform.cleaned_data['username']
            password = userform.cleaned_data['password']
            email = userform.cleaned_data['email']
            User.objects.create(username=username,password=password,email=email) #保存到数据库里
            User.save()

            return HttpResponse('regist success!!!')
    else:
        userform = UserForm()
    return render_to_response('regist.html',{'userform':userform})

def login(request):
    if request.method == 'POST':
        userform = UserForm(request.POST)
        if userform.is_valid():
            username = userform.cleaned_data['username']
            password = userform.cleaned_data['password']

            user = User.objects.filter(username__exact=username,password__exact=password)

            if user:
                return render_to_response('home.html',{'userform':userform})
            else:
                return HttpResponse('用户名或密码错误,请重新登录')

    else:
        userform = UserForm()
    return render_to_response('login1.html',{'userform':userform})
